import cgi
import os
import datetime

from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.api import users
from droidguide_classes import *

def getUserContextFromDB(self):
  droidGuideUser_key = self.request.get(DROID_GUIDE_USER_KEY)
  if droidGuideUser_key:
      # getting the user context in the server if it exists
      q = UserContext.gql("WHERE droidGuideUser_key = :dgkey",
           dgkey=droidGuideUser_key)
      userContext = q.get()
      if userContext:
          return userContext
      else:
          return None

def saveUserContext(self):
  signature = self.request.get('signature')
  droidGuideUser_key = self.request.get(DROID_GUIDE_USER_KEY)
  v = signature.split(',')
  hunger = int(v[0])
  mood = int(v[1])
  provision = int(v[2])
  sleep = int(v[3])
  time = int(v[4])

  userContext = getUserContextFromDB(self)
  if userContext:
          userContext.hunger = hunger
	  userContext.mood = mood
	  userContext.provision = provision
	  userContext.sleep = sleep
	  userContext.time = time
	  userContext.put()
  else:
          userContext = UserContext()
	  userContext.droidGuideUser_key = droidGuideUser_key
          userContext.hunger = hunger
	  userContext.mood = mood
	  userContext.provision = provision
	  userContext.sleep = sleep
	  userContext.time = time
	  userContext.put()
  self.response.out.write(userContext.to_xml())      

def deleteUserContext(self):
  userContext = getUserContextFromDB(self)

  droidGuideUser_key = self.request.get(DROID_GUIDE_USER_KEY)
  if userContext:
      userContext.delete()
  elif self.request.get('key'):
      deleteObject(self.request.get('key'))
  else:
      addMessage('User Context not found', self)

def getUserContext(self):
  userContext = getUserContextFromDB(self)
  if userContext:
      self.response.out.write(userContext.to_xml())
  else:
      addMessage('User Context not found', self)

class Process(webapp.RequestHandler):
  def get(self):
      
      operation = self.request.get('operation')

      startDroidGuideMessage(self)

#http://localhost:8080/context/process?operation=save&droidGuideUser_key=agpkcm9pZGd1aWRlchULEg5Ecm9pZEd1aWRlVXNlchjwAgw&signature=1,2,3,4,2
      if operation == 'save':
        saveUserContext(self)

#http://localhost:8080/context/process?operation=get&droidGuideUser_key=agpkcm9pZGd1aWRlchULEg5Ecm9pZEd1aWRlVXNlchjwAgw
      if operation == 'get':
        getUserContext(self)

      elif operation == 'delete':
        deleteUserContext(self)
          
      elif operation == 'list':
        listObjects(UserContext, self)

      elif operation == 'clear':
        deleteAll(UserContext)

      finishDroidGuideMessage(self)

application = webapp.WSGIApplication([('/context/process', Process)], debug=True)


def main():
  run_wsgi_app(application)

if __name__ == '__main__':
  main()
